package com.meng.baselibrary.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/1.
 */

public abstract class BaseRecycleViewAdapter<T> extends RecyclerView.Adapter{
    private List<T> datas = new ArrayList<>();
    private LayoutInflater inflater;
    private Context context;
    public List<T> getDatas() {
        return datas;
    }
    public void setDatas(List<T> datas){
        this.datas.clear();
        this.datas.addAll(datas);
        this.notifyDataSetChanged();
    }
    public void addDatas(List<T> datas){
        this.datas.addAll(datas);
        this.notifyDataSetChanged();
    }
    public void insertDatas(List<T> datas){
        this.datas.addAll(0,datas);
        this.notifyDataSetChanged();
    }
    /**
     * 局部刷新
     * @param position item的位置不包括header
     */
    public void notifyRealItemChanged(int position){
        super.notifyItemChanged(position+getHeaderViewCount());
    }
    public void clearDatas(){
        datas.clear();
        notifyDataSetChanged();
    }
    public T getItem(int position){
        return datas.get(position);
    }
    protected View inflaterView(int layoutRes, ViewGroup viewGroup){
        return getInflater().inflate(layoutRes,viewGroup,false);
    }
    public LayoutInflater getInflater() {
        return inflater;
    }
    public Context getContext() {
        return context;
    }
    private List<View> headers = new ArrayList<>();
    private List<View> footers = new ArrayList<>();
    public void addHeaderView(View view){
        if(!headers.contains(view)){
            headers.add(view);
        }
    }
    public void addFooterView(View view){
        if(!footers.contains(view)){
            footers.add(view);
        }
    }
    public int getHeaderViewCount(){
        return headers.size();
    }
    public int getFooterViewCount(){
        return footers.size();
    }
    public BaseRecycleViewAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }
    @Override
    public int getItemViewType(int position) {
        if(position >= getHeaderViewCount() && position <  getHeaderViewCount()+datas.size()){
            return -1;
        }
        return position;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == -1){
            return onCreateItemViewHolder(parent,viewType);
        }
        if(viewType < getHeaderViewCount()){
            return new ViewHolder(headers.get(viewType));
        }
        return new ViewHolder(footers.get(viewType-getHeaderViewCount()-datas.size()));
    }

    protected abstract RecyclerView.ViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType);
    protected abstract void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position);
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(getItemViewType(position) == -1){
            onBindItemViewHolder(holder,position-getHeaderViewCount());
        }
    }

    @Override
    public int getItemCount() {
        return getHeaderViewCount()+datas.size()+getFooterViewCount();
    }
    private class ViewHolder extends RecyclerView.ViewHolder{
        public ViewHolder(View itemView) {
            super(itemView);
        }
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder{

        public ItemViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    if(onItemClickListener != null){
//                        int realPos = getAdapterPosition()-getHeaderViewCount();
                        int realPos = getAdapterPosition();
                        T bean =  getItem(realPos);
                        onItemClickListener.onItemClick(getAdapterPosition(),realPos,v,bean);
                    }
                }
            });
        }
    }
    public interface OnItemClickListener<T>{
        void onItemClick(int recycleViewPosition, int itemPosition, View view, T data);
    }
    private OnItemClickListener onItemClickListener;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }
}
